package com.hctforgreen.greenservice.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.os.HandlerThread;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hctforgreen.greenservice.MessageDetailActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.db.DBMessageManager;
import com.hctforgreen.greenservice.model.BaseEntity;
import com.hctforgreen.greenservice.model.MessagesListEntity.MessagesEntity;
import com.hctforgreen.greenservice.ui.handler.QueryMessageListHandler;
import com.hctforgreen.greenservice.ui.page.AbstractDataLoaderHandler;
import com.hctforgreen.greenservice.ui.page.AbstractPageableAdapter;
import com.hctforgreen.greenservice.ui.widget.PullToRefreshListView;
import com.hctforgreen.greenservice.ui.widget.PullToRefreshListView.OnRefreshListener;
import com.hctforgreen.greenservice.utils.HctConstants;

public class QueryMessageListAdapter extends
		AbstractPageableAdapter<BaseEntity> implements OnItemClickListener,
		OnRefreshListener {

	private Activity mActivity;
	private View mConvertView;
	private PullToRefreshListView mListView;
	private QueryMessageListHandler mHandler;

	public QueryMessageListAdapter(PullToRefreshListView listView,
			Activity activity, View convertView, int loadingViewResourceId,
			int reloadViewResourceId,
			AbstractDataLoaderHandler<BaseEntity> handler) {
		super(listView, activity, loadingViewResourceId, reloadViewResourceId,
				handler);
		mListView = listView;
		mActivity = activity;
		mConvertView = convertView;

		mHandler = (QueryMessageListHandler) handler;
		mListView.setAdapter(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnScrollListener(this);

		mListView.setOnRefreshListener(this);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final MessagesEntity entity = (MessagesEntity) getItem(position);
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_recent_message_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (ViewCache) convertView.getTag();
		}
		viewCache.getDateTv().setText(entity.date);
		viewCache.getMsgTitleTv().setText(entity.msgTitle);
		if (0 == entity.readStatus) {
			viewCache.getReadedStatusCb().setChecked(false);
		} else {
			viewCache.getReadedStatusCb().setChecked(true);
		}
		return convertView;
	}

	class ViewCache {
		private View baseView;
		private TextView date;
		private TextView msgTitle;
		private CheckBox readedStatusCb;

		public ViewCache(View view) {
			baseView = view;
		}

		public TextView getDateTv() {
			if (date == null) {
				date = (TextView) baseView.findViewById(R.id.tv_date);
			}
			return date;
		}

		public TextView getMsgTitleTv() {
			if (msgTitle == null) {
				msgTitle = (TextView) baseView.findViewById(R.id.tv_msg_title);
			}
			return msgTitle;
		}

		public CheckBox getReadedStatusCb() {
			if (readedStatusCb == null) {
				readedStatusCb = (CheckBox) baseView
						.findViewById(R.id.cb_message_readed_status);
			}
			return readedStatusCb;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			final int position, long id) {
		final MessagesEntity entity = (MessagesEntity) getItem(position - 1);
		// Intent intent = new Intent();
		// intent.setClass(mActivity, MessageDetailActivity.class);
		// intent.putExtra(HctConstants.ON_ACTIVITY_KEY_MESSAGES_ENTITY,
		// entity);
		// mActivity.startActivity(intent);

		// final MessagesEntity entity = (MessagesEntity) getItem(position);

		new HandlerThread("update_status") {
			public void run() {
				try {
					if (entity.readStatus != 1) {

						entity.readStatus = 1;// 设置为已读
						DBMessageManager dbMessageManager = new DBMessageManager(
								mActivity);
						dbMessageManager.updateMsg(entity);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

				mActivity.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						getmList().set(position - 1, entity);
						notifyDataSetChanged();

						Intent intentB = new Intent();
						intentB.setAction(RecentMessageListAdapter.FRESH_MSG_DATA);
						intentB.putExtra("msg", entity);
						mActivity.sendBroadcast(intentB);

						Intent intent = new Intent();
						intent.setClass(mActivity, MessageDetailActivity.class);
						intent.putExtra(
								HctConstants.ON_ACTIVITY_KEY_MESSAGES_ENTITY,
								entity);
						mActivity.startActivity(intent);
					}
				});
			};
		}.start();

	}

	@Override
	public void onRefresh() {
		mHandler.refreshTop(System.currentTimeMillis());
	}

}
